package cate.game.role.mall.refresh.impl;

import cate.game.GameBody;
import cate.game.role.mall.RefreshTimeCheck;
import cate.game.role.mall.refresh.item.RefreshItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;

public interface IPayRefresh {
    RefreshItem getPayRefreshItem();
    String getPayCycle();

    int getPayTimeMax();

    /** 商店有偿刷新重置周期检测 */
    @JsonIgnore
    default void checkPayCycle(GameBody game) {
        String payCycle = getPayCycle();
        int payTimeMax = getPayTimeMax();
        // 看看这个商店有没有有偿刷新重置周期
        if (StringUtils.isBlank(payCycle) || payTimeMax <= 0) {
            return;
        }
        RefreshItem refreshItem = getPayRefreshItem();
        if (refreshItem == null) {
            return;
        }
        if (RefreshTimeCheck.isRefresh(game, refreshItem.getLastRefreshTime(), payCycle)) {
            refreshItem.updateRefreshItem(System.currentTimeMillis(), payTimeMax);
        }
    }

    default boolean hasPayTimes() {
        return getPayRefreshItem() != null && getPayRefreshItem().times > 0;
    }

    default void addPayTimes(int payTimes) {
        RefreshItem item = getPayRefreshItem();
        if (item != null) {
            item.addTimes(payTimes);
        }
    }

    default void costPayTimes() {
        RefreshItem item = getPayRefreshItem();
        if (item != null) {
            item.costTimes();
        }
    }
}
